package com.sjy.project.fym.api;

import java.math.BigDecimal;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import com.sjy.framework.aspectj.lang.annotation.Log;
import com.sjy.framework.aspectj.lang.enums.BusinessType;
import com.sjy.project.fym.place.domain.FyPlace;
import com.sjy.project.fym.place.service.IFyPlaceService;
import com.sjy.project.fym.signin.domain.FyUserSignin;
import com.sjy.project.system.user.domain.User;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import springfox.documentation.annotations.ApiIgnore;

import com.sjy.framework.web.controller.BaseController;
import com.sjy.framework.web.domain.AjaxResult;
import com.sjy.common.constant.UserContext;
import com.sjy.common.entity.SecuryUserDetail;
import com.sjy.common.exception.BusinessException;
import com.sjy.common.result.BaseResponse;
import com.sjy.common.utils.StringUtils;
import com.sjy.common.utils.poi.ExcelUtil;
import com.sjy.framework.web.page.TableDataInfo;

/**
 * 防疫点信息Controller
 * 
 * @author sjy
 * @date 2022-04-17
 */
@RestController
@Api(value = "经开区防疫场所", description = "防疫场所相关接口", tags = {"经开区防疫"})
@RequestMapping("/api/fym/place")
public class ApiFyPlaceController extends BaseController
{

    @Autowired
    private IFyPlaceService fyPlaceService;
    
    
    /**
     * 新增保存防疫点信息
     */
    @ApiOperation(value = "301、新增场所", position = 301, notes = "")
    @ApiImplicitParams({
	  @ApiImplicitParam(name = "placeName", value = "核酸采样点名称", required = false, example = "海城"),
	  @ApiImplicitParam(name = "zone", value = "行政区划", required = false, example = "温州"),
	  @ApiImplicitParam(name = "community", value = "社区", required = false, example = "xx"),
	  //@ApiImplicitParam(name = "lnglat", value = "点位", required = false, example = "120,27.1"),
	  @ApiImplicitParam(name = "lng", value = "经度", required = false, example = "120"),
	  @ApiImplicitParam(name = "lat", value = "纬度", required = false, example = "27.1"),
	  @ApiImplicitParam(name = "addr", value = "采样点具体位置", required = false, example = "xx"),
	  @ApiImplicitParam(name = "mgrName", value = "指挥长", required = false, example = "林"),
	  @ApiImplicitParam(name = "mgrPhone", value = "手机号", required = false, example = "158"),
	  @ApiImplicitParam(name = "coverNum", value = "覆盖人数", required = false, example = "158"),
	  @ApiImplicitParam(name = "ygcydyzs", value = "手机号", required = false, example = "158"),
	  @ApiImplicitParam(name = "ygcytdrs", value = "采样团队人数", required = false, example = "158"),
	  @ApiImplicitParam(name = "signInRadius", value = "签到打卡半径 单位 米", required = false, example = "158"),
	  @ApiImplicitParam(name = "status", value = "状态 1=正常,2=备用", required = false, example = "1"),
		 
	 })
    @PostMapping("/add")
    public ResponseEntity<BaseResponse> addSave(@ApiIgnore FyPlace fyPlace)
    {
    	//检测是否有重名place
    	FyPlace exist = fyPlaceService.selectPlaceByPlaceName(fyPlace.getPlaceName());
    	if(exist!=null) {
    		return BaseResponse.generateBadResponseEntity("新增失败-当前名称已存在", fyPlace);
    	}

    	SecuryUserDetail user = UserContext.getUserSession();
    	fyPlace.setCreateBy(user.getLoginName());
    	
    	int flag = fyPlaceService.insertFyPlace(fyPlace);
    	if(flag>0) {
        	return BaseResponse.generateOKResponseEntity("新增成功"); 
    	}
    	return BaseResponse.generateBadResponseEntity("新增失败", fyPlace);
    }
    
    /**
     * 删除防疫点信息
     */
    @Log(title = "防疫点信息", businessType = BusinessType.DELETE)
    @ApiOperation(value = "302、删除防疫点信息", position = 302, notes = "英文半角逗号分割")
    @ApiImplicitParams({
	  @ApiImplicitParam(name = "ids", value = "id ,拼接字符串", required = false, example = "海城"),
		 
	 })
    @PostMapping( "/remove")
    public ResponseEntity<BaseResponse<String>> remove(String ids)
    {
    	int flag = fyPlaceService.deleteFyPlaceByPlaceIds(ids);
    	if(flag>0) {
        	return BaseResponse.generateOKResponseEntity("删除成功"); 
    	}
        return BaseResponse.generateBadResponseEntity("删除失败", ids);
    }
    
    /**
     * 修改保存防疫点信息
     */
    @Log(title = "防疫点信息", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ApiOperation(value = "303、修改防疫点信息", position = 303, notes = "")
    @ApiImplicitParams({
  	  @ApiImplicitParam(name = "placeId", value = "核酸采样点名称", required = true, example = "海城"),
	  @ApiImplicitParam(name = "placeName", value = "核酸采样点名称", required = false, example = "海城"),
	  @ApiImplicitParam(name = "zone", value = "行政区划", required = false, example = "温州"),
	  @ApiImplicitParam(name = "community", value = "社区", required = false, example = "xx"),
	  //@ApiImplicitParam(name = "lnglat", value = "点位", required = false, example = "120,27.1"),
	  @ApiImplicitParam(name = "lng", value = "经度", required = false, example = "120"),
	  @ApiImplicitParam(name = "lat", value = "纬度", required = false, example = "27.1"),
	  @ApiImplicitParam(name = "addr", value = "采样点具体位置", required = false, example = "xx"),
	  @ApiImplicitParam(name = "mgrName", value = "指挥长", required = false, example = "林"),
	  @ApiImplicitParam(name = "mgrPhone", value = "手机号", required = false, example = "158"),
	  @ApiImplicitParam(name = "coverNum", value = "覆盖人数", required = false, example = "158"),
	  @ApiImplicitParam(name = "ygcydyzs", value = "手机号", required = false, example = "158"),
	  @ApiImplicitParam(name = "ygcytdrs", value = "采样团队人数", required = false, example = "158"),
	  @ApiImplicitParam(name = "signInRadius", value = "签到打卡半径 单位 米", required = false, example = "158"),
	  @ApiImplicitParam(name = "status", value = "状态 1=正常,2=备用", required = false, example = "1"),
		 
	 })
    public ResponseEntity<BaseResponse> editSave(@ApiIgnore FyPlace fyPlace)
    {
    	if(fyPlace.getPlaceId()==null) {
        	return BaseResponse.generateBadResponseEntity("修改的时候placeId不能为空", null);
    	}
    	SecuryUserDetail user = UserContext.getUserSession();
    	fyPlace.setUpdateBy(user.getLoginName());
    	int flag = fyPlaceService.updateFyPlace(fyPlace);
    	if(flag>0) {
        	return BaseResponse.generateOKResponseEntity("修改成功"); 
    	}
        return BaseResponse.generateBadResponseEntity("修改失败", null);
    }

    
    @ApiOperation(value = "304、获取场所清单", position = 304, notes = "当前页:pageNum,分页大小：pageSize,排序字段：orderByColumn，排序方式：asc")
    @ApiImplicitParams({
  	  @ApiImplicitParam(name = "placeId", value = "核酸采样点编号", required = false, example = "200"),
  	  @ApiImplicitParam(name = "placeName", value = "核酸采样点名称", required = false, example = "海城"),
  	  @ApiImplicitParam(name = "zone", value = "行政区划", required = false, example = "温州"),
  	  @ApiImplicitParam(name = "community", value = "社区", required = false, example = "xx"),
  	  @ApiImplicitParam(name = "addr", value = "采样点具体位置", required = false, example = "xx"),
  	  @ApiImplicitParam(name = "mgrName", value = "指挥长", required = false, example = "林"),
  	  @ApiImplicitParam(name = "mgrPhone", value = "手机号", required = false, example = "158"),
  	  @ApiImplicitParam(name = "status", value = "状态 1=正常,2=备用", required = false, example = "1"),
  	  @ApiImplicitParam(name = "pageNum", value = "当前页", required = false, example = "1"),
  	  @ApiImplicitParam(name = "pageSize", value = "分页大小", required = false, example = "15"),
  	  @ApiImplicitParam(name = "orderByColumn", value = "排序字段", required = false, example = "create_time"),
  	  @ApiImplicitParam(name = "isAsc", value = "排序的方向desc或者asc", required = false, example = "asc"),
  	 
    })
    @PostMapping({"/list"})
    public ResponseEntity<BaseResponse<TableDataInfo<FyPlace>>> list(@ApiIgnore FyPlace fyPlace)
    {
        startPage();
        List<FyPlace> list = fyPlaceService.selectFyPlaceList(fyPlace);
        return BaseResponse.generateOKResponseEntity(getDataTable(list));
    }
    
    @ApiOperation(value = "305、根据placeId获取场所信息详情 包含队伍人员", position = 305, notes = "")
    @ApiImplicitParams({
  	  @ApiImplicitParam(name = "placeId", value = "场所编号", required = false, example = "1")
    })
    @PostMapping({"/getPlaceByPlaceId"})
    public ResponseEntity<BaseResponse<FyPlace>> getPlaceByPlaceId(Long placeId) throws Exception
    { 
    	FyPlace place = fyPlaceService.selectFyPlaceByPlaceId(placeId);
    	if(place != null) {
    		return BaseResponse.generateOKResponseEntity(place);
    	}
    	return BaseResponse.generateBadResponseEntity(500,"获取场所信息失败", place);
    	
    }
    /**
     * 导入数据
     */
    @RequiresPermissions("fym:place:import")
    @Log(title = "防疫点信息", businessType = BusinessType.IMPORT)
    @PostMapping("/importData")
    @ResponseBody
    public AjaxResult importData(MultipartFile file, boolean updateSupport) throws Exception
    {
        ExcelUtil<FyPlace> util = new ExcelUtil<FyPlace>(FyPlace.class);
        List<FyPlace> list = util.importExcel(file.getInputStream());
        String message = fyPlaceService.importPlace(list, updateSupport);
        return AjaxResult.success(message);
    }
    @RequiresPermissions("fym:place:view")
    @GetMapping("/importTemplate")
    @ResponseBody
    public AjaxResult importTemplate()
    {
        ExcelUtil<FyPlace> util = new ExcelUtil<FyPlace>(FyPlace.class);
        return util.importTemplateExcel("防疫点数据");
    }
    /**
     * 导出防疫点信息列表
     */
    @RequiresPermissions("fym:place:export")
    @Log(title = "防疫点信息", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(FyPlace fyPlace)
    {
        List<FyPlace> list = fyPlaceService.selectFyPlaceList(fyPlace);
        ExcelUtil<FyPlace> util = new ExcelUtil<FyPlace>(FyPlace.class);
        return util.exportExcel(list, "防疫点信息数据");
    }

    
}
